package lab2.why;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class WordFreq {
	
	public static void main(String[] args) {
		 
		File dest = new File("D:/JavaWorkspace/example/src/lab2/output.txt");
		Map<String,Integer> map = new HashMap<>();
		
		String str = readFromTxt_Reader("D:/JavaWorkspace/example/src/lab2/了不起的盖茨比英文.txt");
		String[] words = str.split("\\s+");
		
	    for(int i = 0 ; i < words.length ; i++) {
	    	if(map.get(words[i]) != null) {
	    		map.put(words[i], map.get(words[i])+1);
	    	}
	    	else {
	    		map.put(words[i], 1);
	    	}
	    }
	    	
	    //排序
	    List<Map.Entry<String,Integer>> list = new ArrayList<Map. Entry<String,Integer> >(map.entrySet());
        Collections.sort(list,new Comparator<Map.Entry<String,Integer>>() {
            public int compare(Entry<String, Integer> o1,
                    Entry<String, Integer> o2) {
                return -o1.getValue().compareTo(o2.getValue());
            }
            
        });
	     
        //用writer输出
        Writer writer = null;
        try {
            Map.Entry<String, Integer> mapSort = null;
			writer = new FileWriter(dest);
			 for (int i = 0 ;i<list.size() ;i++) {        	
		        	mapSort = list.get(i); 
		        	writer.write(mapSort.getKey() + " " + mapSort.getValue() + "\n");
		        }
			 System.out.println("OK");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  	
	}
	
	//用reader读入
	public static String readFromTxt_Reader(String filename) {
		Reader reader = null;
		StringBuffer buf = new StringBuffer();
		try {
			reader = new FileReader(filename);
			char[] chars = new char[1024*10];
			
			
			int len = -1;
			while((len = reader.read(chars)) != -1) {
				String str = new String(chars , 0 , len);
				buf.append(str);	
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
				if(reader!=null) {
					
					try {
						reader.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
		}
		return buf.toString();
	}
	
	//用InputStream读入
	public static String readFromTxt_InputStream(String filename) {
		InputStream is = null;
		StringBuffer buf = new StringBuffer();
		try {
			is = new FileInputStream(filename);
			byte[] flush = new byte[1024*10];
			
			int len = -1;
			while((len = is.read(flush)) != -1) {
				String str = new String(flush , 0 , len);
				buf.append(str);	
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
				if(is!=null) {
					
					try {
						is.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
		}
		return buf.toString();
	}
}
